package com.changgou.goods.controller;

import com.changgou.entity.PageResult;
import com.changgou.entity.Result;
import com.changgou.entity.StatusCode;
import com.changgou.goods.pojo.Brand;
import com.changgou.goods.service.BrandService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/brand")
@CrossOrigin //解决跨域问题
public class BrandController {

    @Autowired
    private BrandService brandService;

    //新增品牌数据
    @PostMapping
    public Result add(@RequestBody Brand brand) {
        brandService.add(brand);
        return new Result(true, StatusCode.OK, "添加成功(●'◡'●)");
    }

    //根据ID删除品牌
    @DeleteMapping(value = "/{id}")
    public Result delete(@PathVariable Integer id) {
        brandService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功(●'◡'●)");
    }

    //修改品牌数据
    @PutMapping/*(value = "/{id}")*/
    public Result update(@RequestBody Brand brand/*,@PathVariable Integer id*/) {
        /*brand.setId(id);*/
        brandService.update(brand);
        return new Result(true, StatusCode.OK, "修改成功(●'◡'●)");
    }

    //查询全部品牌
    @GetMapping
    public Result findAll() {
        List<Brand> brandList = brandService.findAll();
        return new Result(true, StatusCode.OK, "查询成功(●'◡'●)", brandList);
    }

    //根据id查询品牌数据
    @GetMapping("/{id}")
    public Result findById(@PathVariable Integer id) {
        Brand brand = brandService.findById(id);
        return new Result(true, StatusCode.OK, "查询成功(●'◡'●)", brand);
    }

    //多条件搜索品牌数据
    @GetMapping(value = "/search")
    public Result findList(@RequestParam Map searchMap) {
        List<Brand> list = brandService.findList(searchMap);
        return new Result(true, StatusCode.OK, "查询成功(●'◡'●)", list);
    }

    //品牌列表分页查询
    @GetMapping(value = "/search/{page}/{size}")
    public Result findPage(@PathVariable int page, @PathVariable int size) {
        Page<Brand> pageList = brandService.findPage(page, size);
        PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());
        return new Result(true, StatusCode.OK, "分页查询成功(●'◡'●)", pageResult);
    }

    //多条件分页查询
    @GetMapping(value = "/searchPage/{page}/{size}")
    public Result findPage(@RequestParam Map searchMap, @PathVariable int page, @PathVariable int size) {
        Page pageList = brandService.findPage(searchMap, page, size);
        PageResult pageResult = new PageResult(pageList.getTotal(), pageList.getResult());
        return new Result(true, StatusCode.OK, "条件分页查询成功(●'◡'●)", pageResult);
    }

    //根据商品分类名称查询品牌列表
    @GetMapping("category/{categoryName}")
    public Result findListByCategoryName(@PathVariable String categoryName){
        List<Map> brandList = brandService.findListByCategoryName(categoryName);
        return new Result(true,StatusCode.OK,"根据商品分类名称查询品牌列表查询成功(●'◡'●)",brandList);
    }

    //根据商品分类名称查询规格
    @GetMapping("/spc/{categoryName}")
    public Result<List<Map>> findSpecListByCategoryName(@PathVariable("categoryName") String categoryName){
        List<Map> specList = brandService.findSpecListCategoryName(categoryName);
        return new Result<>(true,StatusCode.OK,"根据商品分类名称查询规格成功(●'◡'●)",specList);
    }
}
